/*!
 @header RegisterRealNameFormViewController.m
 @author 周益
 @version 1.0.0 2013/01/28 Creation
 Copyright (c) 2013年 mypocketbaby. All rights reserved.
 */

//  -业务逻辑细节说明-
//
//  @abstract       RegisterRealNameFormViewController功能和实现较为简单，将所有的数据一起向服务器请求。
//  	<实现描述>：请求成功后，会保存用户的信息到本地，并且自动登录。
//
//  @discussion

//  @notes          


//  -类修改日志-
//  修改人   修改时间      针对版本         修改描述

//  周益    2013-4-9      1.2.1       为注册请求加入地区

#import "RegisterRealNameFormViewController.h"
#import "Config.h"
#import "Helper.h"
#import "LaunchViewController.h"
#import "MainViewController.h"
#import "JMFBase.h"
#import "Option.h"
#import "DBKit.h"


@interface RegisterRealNameFormViewController ()

@end

@implementation RegisterRealNameFormViewController
@synthesize avatar = _avatar;
@synthesize area = _area;
@synthesize collage = _collage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //背景
    self.view.backgroundColor = _SYS_COLOR_BACKGROUND;
    
    //标题
    self.title = _L(@"vc_title_056");
    
    //导航按钮：返回
    UIButton *backButton = [Helper buttonWithStyle:ButtonStyleNavigationReturn];
    [backButton addTarget:self action:@selector(backButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    //输入框背景
    UIImageView *textFieldImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"com_bg_010"]];
    textFieldImageView.frame = CGRectMake(10, 20, 300, 45);
    [self.view addSubview:textFieldImageView];
    
    //输入框
    _textField = [Helper textField:CGRectMake(15, 21, 295, 40) byPlaceholder:_L(@"holder_13")];   //请填写你的真实姓名
    _textField.font = _SYS_FONT_009;
    _textField.keyboardType = UIKeyboardTypeDefault;
    [_textField becomeFirstResponder];
    [self.view addSubview:_textField];
    
    //完成注册按钮
    UIButton *finishButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 110, 300, 44)];
    [finishButton setBackgroundImage:[UIImage imageNamed:@"com_btn_006_n"] forState:UIControlStateNormal];
    [finishButton setTitle:_L(@"btn_yes_15") forState:UIControlStateNormal];   //完成注册
    [finishButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    finishButton.titleLabel.font = _SYS_FONT_009;
    [finishButton addTarget:self action:@selector(finishButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:finishButton];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 自定义事件

/*!
 @abstract 返回按钮事件
 */
- (void)backButtonEvent:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

/*!
 @abstract 完成按钮事件
 */
- (void)finishButtonEvent:(id)sender
{
    
    NSUserDefaults *userInfo = [NSUserDefaults standardUserDefaults];
    NSString *mobile     = [userInfo objectForKey:@"accountMobile"];
    NSString *password   = [userInfo objectForKey:@"accountPassword"];
    NSString *code       = [userInfo objectForKey:@"code"];
    NSString *realName   = _textField.text;
    Boolean sex          = [userInfo boolForKey:@"sex"];
    NSString *gender     = sex ? @"true" : @"false";
    
    //压缩图片
    UIImage *newImage = [Helper resizeImage:_avatar forStyle:PictureStyleNormal];
    NSData *imageData = [Helper compressImageData:newImage withStyle:PictureStyleNormal];
    
    //
    JSONRequest* request = [[JSONRequest alloc] initWithURLString:_JSON_URL_U_ADD];
    request.delegate = self;
    request.hudOn = true;
    request.hudTitle = _L(@"title_loading_05");
    request.requestMode = RequestModeMultiPart;

    [request setValue:mobile forParameter:@"mobile"];
    [request setValue:password forParameter:@"password"];
    [request setValue:code forParameter:@"code"];
    [request setValue:realName forParameter:@"realName"];
    [request setValue:gender forParameter:@"sex"];
    [request setValue:imageData forParameter:@"avatr"];
    
    if ([_CONFIG_APP_KEY isEqualToString:@"XYSHQ"]) {
        [request setValue:_collage forParameter:@"location"];

    }
    else {
        [request setValue:_area forParameter:@"location"];
    }
    
    [request asynchronous];
}


#pragma mark - JSONRequestDelegate

//
- (void)requestDidLoadJSON:(NSDictionary *)responseData
{
    JMFBase* jmf = [[JMFBase alloc] initWithJSONDictionary:responseData];

    //自动完成登录，跳转到圈子商讯
    if (jmf.status == JMStatusSucceed) {
    
        //删除暂存的信息
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"code"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"sex"];
        
        //将用户的ID、token存入全局
        [Helper setUserDefaultsWithID:[jmf stringValue:@"id"] andToken:[jmf stringValue:@"token"] andName:[jmf stringValue:@"realName"]];
        
        //将用户ID、mobiel、password存入数据库
        Option* option = [[Option alloc] init];
        
        option.name = @"accountID";
        option.value = [jmf stringValue:@"id"];
        [[DBKit sharedDBKit] save:option];
        
        option.name = @"accountMobile";
        option.value = [[NSUserDefaults standardUserDefaults] stringForKey:@"accountMobile"];
        [[DBKit sharedDBKit] save:option];
        
        option.name = @"accountPassword";
        option.value = [[NSUserDefaults standardUserDefaults] stringForKey:@"accountPassword"];
        [[DBKit sharedDBKit] save:option];
        
        [self.navigationController popToRootViewControllerAnimated:true];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Start_Main" object:nil];
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
    }
    else {
        [Helper easyShowJMFStatus:jmf];
    }
}


//
- (void)requestFailWithNetworkUnavailabe
{
    [Helper showAlertWithTitle:nil andMessage:_L(@"title_networkunavailabe")];
}


@end
